package view;
import java.sql.*;
import java.io.*;

public class CriaBanco {
	private static String createTables (String file, Statement q) {
		String querys = "";
		
		try {
			BufferedReader input = new BufferedReader(new FileReader(file));
			String line;

			while((line = input.readLine()) != null) {
				if (line.lastIndexOf(";") != -1) {
					querys += ")";
					try {
						q.executeUpdate(querys);
					}
					catch(SQLException e) {
						System.err.println(e.getMessage());
					}
					querys = "";
				}
				else {
					querys += line + '\n';
				}
			}
		}
		catch(FileNotFoundException e) {
			System.err.println(e.getMessage());
		}
		catch(IOException e) {
			System.err.println(e.getMessage());
		}
			
		return querys;
	}
	
	public static void main(String[] args) {
		try {
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
		
			Connection connector = DriverManager.getConnection("jdbc:derby:snakes;create=true;");
			Statement query = connector.createStatement();
			
			createTables("SnakesDB.sql", query);
			
			query.close();
			connector.close();
			
			System.out.println("Database successfully created.");
		}
		catch(ClassNotFoundException e) {
			System.err.println(e.getMessage());
		}
		catch(SQLException e) {
			System.err.println(e.getMessage());
		}
	}
}